# Install and load required R packages -----------------------------------------
#   This file is called at the start of the main.R to populate the working
#   environment with the required R packages. First, it checks if the required 
#   packages are installed. If they are not, it installs the packages into the
#   system. Second, the code load required packages to the current working
#   environment.

# Install required packages if not yet installed
pkg_required <- c("Hmisc", "MASS", "nnet", "dplyr", "stringr", "ggplot2", "ggpubr", 
                  "broom.helpers", "ggstats", "marginaleffects", "modelsummary", 
                  "tinytable")
pkg_installed <- rownames(installed.packages())

install.packages(setdiff(pkg_required, pkg_installed))

# Load required packages
invisible(lapply(pkg_required, library, character.only = TRUE))

# Set global options for tinytable
options(tinytable_theme_placement_latex_float = "H")

# Clean environment
rm(pkg_required, pkg_installed)